package com.codemart.lanze.core.freemarker;

import java.io.IOException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.codemart.lanze.core.freemarker.utils.Freemarker;
import com.codemart.lanze.modules.cms.entity.Content;
import com.codemart.lanze.modules.cms.service.IContentService;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.WrappingTemplateModel;

/**
 * 内容详情指令
 *
 * @author lanze
 */
@Repository
public class DirectiveContentView implements TemplateDirectiveModel {

	@Autowired
	private IContentService contentService;

	@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
	@Override
	public void execute(Environment env, Map map, TemplateModel[] loopVars, TemplateDirectiveBody body)
			throws TemplateException, IOException {
		try {
			Integer contentId = Freemarker.getInteger(map, "contentId");
			Content content = contentService.getById(contentId);

			loopVars[0] = WrappingTemplateModel.getDefaultObjectWrapper().wrap(content);
			body.render(env.getOut());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
